﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DLL.Entities;
using DLL.Entities.Config;
using DLL.Entities.Event;
using DLL.Entities.Status;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace MIS
{
    public partial class Form1 : Form
    {
        COMEvent targetpackage;
        MISEvent result;
        MissileObj[] missiles;
        string XMLdir = "..\\..\\..\\DLL\\XML\\";
        string Logdir = "..\\..\\..\\DLL\\Logs\\";
        int distancefeet = 0, id;

        

        public Form1()
        {
            InitializeComponent();
        }

        public void Form1_Load(object sender, EventArgs e)
        {
            txt_ID.Text = "";
            txt_Type.Text = "";
            txt_Speed.Text = "";
            txt_Altitude.Text = "";
            txt_Heading.Text = "";
            txt_Heading.Text = "";
            txt_TargetDistance.Text = "";

            //creates generic object test and Config object RADCON
            object test = new object();
            Config MISCON = new Config();
            // starts timer to send status updates
            txt_SystemStatus.Text = "Idle";
            txt_InterceptStatus.Text = "";
            timer1.Start();
            timer2.Start();
                try
                {
                    //creates serializer of type Config, calls ReadXML() and deserializes RAD_Config.xml to return generic object "test"
                    XmlSerializer serializer = new XmlSerializer(typeof(Config));
                    Read_Write.ReadXML(out test, "MIS_Config.xml", serializer);
                    // casts test to type Config and assigns to RADCON
                    MISCON = (Config)test;
                }

                catch (Exception ex)
                {
                    // writes exceptions to log file
                    Utility.LogFile(ex.ToString());
                }
                //sets model and version text
                txt_Model.Text = MISCON.Model;
                txt_Version.Text = MISCON.Version;
            
                try
                {
                    //creates serializer of type Missile[], calls ReadXML() and deserializes Missile.xml to return generic object "test"
                    XmlSerializer serializer = new XmlSerializer(typeof(MissileObj[]));
                    Read_Write.ReadXML(out test, "Missile.xml", serializer);
                    // casts test to type Missile[] and assigns to missiles
                    missiles = (MissileObj[])test;

                    txt_aaCount.Text = missiles[0].count.ToString();
                    txt_amCount.Text = missiles[1].count.ToString();
                    txt_asCount.Text = missiles[2].count.ToString();
                }

                catch (Exception ex)
                {
                    // writes exceptions to log file
                    Utility.LogFile(ex.ToString());
                }         
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            MISStatus stat = new MISStatus();
            stat.timestamp = DateTime.Now;
            stat.aacount = missiles[0].count;
            stat.amcount = missiles[1].count;
            stat.ascount = missiles[2].count;

            XmlSerializer serializer = new XmlSerializer(typeof(MISStatus));
            Read_Write.WriteXML(stat, "MIS_Status.xml", serializer);
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            if (File.Exists(XMLdir + "COM_Event.xml"))
            {
                targetpackage = new COMEvent();
                try
                {
                    object test = new object();
                    XmlSerializer serializer = new XmlSerializer(typeof(COMEvent));
                    Read_Write.ReadXML(out test, "COM_Event.xml", serializer);
                    targetpackage = (COMEvent)test;

                    File.Move((XMLdir + "COM_Event.xml"), (Logdir + "COM_Event" + Utility.Random(1, 10000000) + ".xml"));

                    txt_ID.Text = targetpackage.id.ToString();
                    txt_Type.Text = targetpackage.target;
                    txt_Speed.Text = targetpackage.speed.ToString();
                    txt_Altitude.Text = targetpackage.altitude.ToString();
                    txt_Heading.Text = targetpackage.heading.ToString();
                    distancefeet = targetpackage.distance * 5280;
                    txt_TargetDistance.Text = distancefeet.ToString();

                    id = targetpackage.id;

                    if (targetpackage.command == "Fire")
                    {
                        txt_SystemStatus.Text = "Engaging";
                        txt_InterceptStatus.Text = "Missile Away";

                        if (targetpackage.missile == "Anti_Air")
                        {
                            missiles[0].count--;
                        }
                        else if (targetpackage.missile == "Anti_Missile")
                        {
                            missiles[1].count--;
                        }
                        else
                        {
                            missiles[2].count--;
                        }

                        txt_aaCount.Text = missiles[0].count.ToString();
                        txt_amCount.Text = missiles[1].count.ToString();
                        txt_asCount.Text = missiles[2].count.ToString();

                        timer3.Start();
                    }
                    if (targetpackage.command == "Abort")
                    {
                        timer3.Stop();
                        result = new MISEvent();
                        txt_SystemStatus.Text = "Idle";
                        txt_InterceptStatus.Text = "Intercept Aborted";
                        result.id = id;
                        result.outcome = "Intercept Aborted";

                        XmlSerializer serializer1 = new XmlSerializer(typeof(MISEvent));
                        Read_Write.WriteXML(result, "MIS_Event.xml", serializer1);
                        
                    }
                }

                catch (Exception ex)
                {
                    Utility.LogFile(ex.ToString());
                }
            }
        }

        private void timer3_Tick(object sender, EventArgs e)
        {
              distancefeet -= 11003;
              
                if (distancefeet <= 0)
                {
                    distancefeet = 0;
                    txt_TargetDistance.Text = distancefeet.ToString();
                    result = new MISEvent();
                    result.id = id;

                    if ((targetpackage.target == "Birds" && targetpackage.missile == "Anti_Air") || (targetpackage.target == "Aircraft" && targetpackage.missile == "Anti_Air") ||
                        (targetpackage.target == "Missile" && targetpackage.missile == "Anti_Missile") || (targetpackage.target == "Ship" && targetpackage.missile == "Anti_Ship"))
                    {
                        result.outcome = "Target Destroyed";
                        txt_SystemStatus.Text = "Idle";
                        txt_InterceptStatus.Text = "Impact on Target";
                    }
                    else
                    {
                        result.outcome = "Intercept Failed";
                        txt_SystemStatus.Text = "Idle";
                        txt_InterceptStatus.Text = "No Impact";
                    }

                    XmlSerializer serializer = new XmlSerializer(typeof(MISEvent));
                    Read_Write.WriteXML(result, "MIS_Event.xml", serializer);

                    timer3.Stop();
                }

                txt_TargetDistance.Text = distancefeet.ToString();
                
            


        }




    }
}
